package com.dhl.observer.improve;

import java.util.ArrayList;
import java.util.List;

/**
 * @title: WeatherData
 * @Author danghailiang
 * @Date: 2022/8/22 10:11
 * @Version 1.0
 */
public class WeatherData implements Subject{
    /**
     * 接收者集合
     */
    List<Observer> observers = new ArrayList<>();
    /**
     * 湿度
     */
    private int temperature;
    /**
     * 湿度
     */
    private int humidity;
    /**
     * 压力
     */
    private int pressure;

    public WeatherData(int temperature, int humidity, int pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
    }

    public int getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    public int getHumidity() {
        return humidity;
    }

    public void setHumidity(int humidity) {
        this.humidity = humidity;
    }

    public int getPressure() {
        return pressure;
    }

    public void setPressure(int pressure) {
        this.pressure = pressure;
    }

    @Override
    public void register(Observer observer) {
        if(observers.contains(observer)){
            return;
        }
        observers.add(observer);
    }

    @Override
    public void remove(Observer observer) {
        if(observers.contains(observer)){
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        display();
        for (Observer observer : observers) {
            observer.update(temperature, pressure, humidity);
        }
    }

    public void display(){
        System.out.println("---------------WeatherData----------");
        System.out.println("**********Today temperature:" + temperature + "***");
        System.out.println("**********Today humidity:" + humidity + "***");
        System.out.println("**********Today pressure:" + pressure + "***");
    }
}
